<template>
  <div>
    <div class="collection-mian">
      <div class="title">我的视频</div>
      <div v-if="isEmpty">
        <div class="collections"  >
          <ul class="collection-ul">
            <li class="collection-li" v-for="(item,index) in videos">
              <div class="collection-li-inner">
                <a href="#" @click="jumpVideo(item)">
                  <div class="img-container">
                    <img :src="'http://localhost:8099/upload/pic/' + item.userId + '-' + item.name + '.jpg'" alt="" loading="lazy">
                  </div>
                </a>
                <div class="inner-options">
                  <a style="width:100%" href="#" @click="jumpVideo(item)">
                    <div class="item-name" :title="item.title"> {{ item.name }} </div>
                  </a>
                  <div class="options btn-delete-collection" @click="removeCollection(index, item.id)" v-if="ismy">
                    删除
                  </div>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div v-else class="empty">
        收藏夹为空~
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyVideo",
  props: ['verifyUser'], //passvalue是父组件传递给子组件的参数
  data(){
    return{
      isEmpty: false,
      videos: [],
      userId: this.verifyUser,
      isMy:true,
    }
  },
  methods: {
    jumpVideo(item){
      let routeUrl = this.$router.resolve({
        path: "/video",
        query:{
          id:item.id,
          assets:item.assets,
          userId:item.userId
        }
      })
      window.open(routeUrl.href, '_blank')
    },
    verify(){
      let user = this.$sessionGet();
      if(user.id === this.userId){
        this.ismy = true
      }else{
        this.ismy = false
      }
    },
    removeCollection(index, id) {
      this.$axios.get("resource/delById?id=" + id + "&sType=1")
          .then(resp=>{
            console.log(resp);
            this.videos.splice(index, 1);
            this.videos.sort();
            alert("删除成功");
          })
          .catch(error=>{
            alert("删除失败")
            console.log(error)
          })
    },
    searchByUserId(){
      this.$axios.get("resource/searchListByUserId?userId=" + this.userId)
          .then(resp=>{
            let res = resp.data
            let length = res.data.length
            if(length !== 0){
              this.isEmpty = true;
            }
            this.videos = res.data;
          })
          .catch(error=>{
            console.log(error)
          })
    }
  },
  created() {
    this.searchByUserId()
    this.verify()
  }
}
</script>

<style lang="css">
.btn-name {
  display: flex;
  justify-content: space-between;
  justify-items: center;
}

a{
  text-decoration: none;
  color: #282626;
}

.visible-switch {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.visible-switch .el-switch {
  --el-switch-on-color: #9ac8e2;
}

.collection-mian {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  background-color: #fff;
  min-height: 300px;
  font-size: 30px;
}

.collections {
  width: 100%;
  border-bottom: 1px solid ;
  padding-top: 15px;
}

.collection-ul {
  width: 100%;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: flex-start;
  padding: 10px;
  margin: 10px 0 10px 0;
  box-sizing: border-box;
  background-color: #fff;
}

.collection-li {
  box-sizing: border-box;
  width: 20%;
  padding: 10px;
}

.collection-li-inner {
  border-radius: 6px;

  /* height: 180px; */
  width: 100%;
  transition: all .3s ease;
}



.img-container {
  width: 100%;
  aspect-ratio: 16/9;
  overflow: hidden;
  border-radius: 6px;
}

.img-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: pointer;
}

.options {
  width: 30%;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  user-select: none;
  border-radius: 6px;
  border: 1px solid #c4c4c4;
  transition: all .3s ease;
  font-size: 15px;
}

.collections span {
  cursor: pointer;
}

.options:hover {
  border: 1px solid #f56c6c;
  color: #f56c6c;
}

.item-name {
  width: 100%;
  height: 50px !important;
  padding: 3px;
  box-sizing: border-box;
  text-align: left;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  text-overflow: ellipsis;
  word-wrap: break-word;
  height: 100%;
  font-size: 17px;
  cursor: pointer;
  transition: color .3s ease;
}

.empty {
  font-size: 10px;
  margin-top: 50px;
}

.item-name:hover {
  color: #409eff;
}

.inner-options {
  padding: 5px 5px 5px 0;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  padding: 10px 0 10px 0;
}

.el-pagination {
  justify-content: center;
}

@media screen and (max-width:550px) {
  .collection-li {
    width: 50%;
  }
}
</style>